package part1basiclock;

import java.util.concurrent.TimeUnit;

// 测试死锁
public class TestLock09 {

    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "lockB";

        new Thread(new ThreadTest(lockA, lockB), "T1").start();
        new Thread(new ThreadTest(lockB, lockA), "T2").start();
    }

}

class ThreadTest implements Runnable {

    private String lockA;
    private String lockB;

    public ThreadTest(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }


    @Override
    public void run() {
        synchronized (lockA) {
            System.out.println(Thread.currentThread().getName() + "lock:" + lockA + "=>get" + lockB);

            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName() + "lock:" + lockB + "=>get" + lockA);
            }
        }
    }
}